let rightmenu = function (element, menus) {

    if ($("body").find(".right-menu").length == 0) {
        $("body").append("<ul class='right-menu'></ul>")
    }
    var menu = $("body").find(".right-menu");

    function menuclick(ele, i) {
        $(ele).click(function () {
            if (menus[i].click) {
                menus[i].click();
            }
            menu.css("display", "none");
            return false;
        })
    }
    element[0].oncontextmenu = function (e) {
        var e = e || window.event;//兼容

        let html = "";
        for (let m of menus) {
            html = html + "<li style='cursor:pointer;'>" + m.label + "</li>"
        }
        menu.html(html);

        let i = 0;
        menu.find("li").each(function () {
            menuclick(this, i);
            i++;
        });

        //单击显示div
        menu.css("display", "block");
        //设置定义
        //判断鼠标坐标是否大于视口宽度-块本身宽度
        var cakLeft = (e.clientX > document.documentElement.clientWidth - menu.offsetWidth) ? (document.documentElement.clientWidth - menu.offsetWidth) : e.clientX;
        var cakTop = (e.clientY > document.documentElement.clientHeight - menu.offsetHeight) ? (document.documentElement.clientHeight - menu.offsetHeight) : e.clientY;
        menu.css("left", cakLeft + "px");
        menu.css("top", cakTop + "px");

        return false;
    }

    document.onclick = function () {
        menu.css("display", "none");
    }
};

export default rightmenu;